package org.web.webdisplay.server.biz.impl;

import org.web.base.domain.InsertGroup;
import org.web.base.domain.ResultDO;
import org.web.webdisplay.sdk.dto.UserPageQueryInfoDTO;
import org.web.webdisplay.sdk.dto.query.QueryUserPageQueryInfoRequest;
import org.web.webdisplay.server.service.UserPageQueryInfoService;

import java.util.*;

import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import jakarta.annotation.Resource;

import org.apache.log4j.Logger;
import org.web.webdisplay.sdk.biz.UserPageQueryInfoBiz;
import org.web.webdisplay.server.domain.query.QueryUserPageQueryInfo;
import org.web.webdisplay.server.domain.UserPageQueryInfoDO;
import org.web.base.domain.exception.ResultMessageEnum;
import org.web.base.domain.helper.*;

import java.util.List;

@Service("userPageQueryInfoBiz")
public class UserPageQueryInfoBizImpl implements UserPageQueryInfoBiz {

    private final Logger logger = Logger.getLogger(UserPageQueryInfoBizImpl.class);

    @Resource
    private UserPageQueryInfoService userPageQueryInfoService;

    @Override
    public ResultDO<List<UserPageQueryInfoDTO>> selectUserPageQueryInfoList(QueryUserPageQueryInfoRequest queryUserPageQueryInfoRequest) {
        ResultDO<List<UserPageQueryInfoDTO>> resultDO = new ResultDO<>();
        try {
            if (queryUserPageQueryInfoRequest == null) {
                throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_EMPTY);
            }
            queryUserPageQueryInfoRequest.checkAndBuildQuery();
            ValidationHelper.validateWithThrow(QueryUserPageQueryInfoRequest.class, queryUserPageQueryInfoRequest);
            QueryUserPageQueryInfo queryUserPageQueryInfo = new QueryUserPageQueryInfo();
            BeanUtils.copyProperties(queryUserPageQueryInfo, queryUserPageQueryInfoRequest);
            List<UserPageQueryInfoDO> selectList = userPageQueryInfoService.selectUserPageQueryInfoList(queryUserPageQueryInfo);
            List<UserPageQueryInfoDTO> list = new ArrayList<>();
            resultDO.setDomain(list);
            for (UserPageQueryInfoDO userPageQueryInfoDO : selectList) {
                UserPageQueryInfoDTO userPageQueryInfoDTO = new UserPageQueryInfoDTO();
                BeanUtils.copyProperties(userPageQueryInfoDTO, userPageQueryInfoDO);
                list.add(userPageQueryInfoDTO);
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ResultHelper.buildResultDOByException(e);
        }
        return resultDO;
    }

    @Override
    public ResultDO<Integer> countUserPageQueryInfoList(QueryUserPageQueryInfoRequest queryUserPageQueryInfoRequest) {
        ResultDO<Integer> resultDO = new ResultDO<>();
        try {
            if (queryUserPageQueryInfoRequest == null) {
                throw ServiceExceptionHelper.buildServiceException(ResultMessageEnum.PARAM_EMPTY);
            }
            ValidationHelper.validateWithThrow(QueryUserPageQueryInfoRequest.class, queryUserPageQueryInfoRequest);
            QueryUserPageQueryInfo queryUserPageQueryInfo = new QueryUserPageQueryInfo();
            BeanUtils.copyProperties(queryUserPageQueryInfo, queryUserPageQueryInfoRequest);
            Integer count = userPageQueryInfoService.countUserPageQueryInfoList(queryUserPageQueryInfo);
            resultDO.setDomain(count);
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ResultHelper.buildResultDOByException(e);
        }
        return resultDO;
    }

    @Override
    public ResultDO<UserPageQueryInfoDTO> saveUserPageQueryInfo(UserPageQueryInfoDTO userPageQueryInfoDTO) {
        ResultDO<UserPageQueryInfoDTO> resultDO = new ResultDO<>();
        try {
            resultDO.setDomain(userPageQueryInfoDTO);
            ValidationHelper.validateWithThrow(UserPageQueryInfoDTO.class, userPageQueryInfoDTO, InsertGroup.class);
            QueryUserPageQueryInfo queryUserPageQueryInfo = new QueryUserPageQueryInfo();
            queryUserPageQueryInfo.setUserId(userPageQueryInfoDTO.getUserId());
            queryUserPageQueryInfo.setQueryName(userPageQueryInfoDTO.getQueryName());
            queryUserPageQueryInfo.setDisplayInfoId(userPageQueryInfoDTO.getDisplayInfoId());
            queryUserPageQueryInfo.setSpecifiedAttributeSet(new HashSet<>(List.of("user_page_query_info_id")));

            UserPageQueryInfoDO userPageQueryInfoDO = userPageQueryInfoService.selectOneUserPageQueryInfo(queryUserPageQueryInfo);
            if (userPageQueryInfoDO != null) {
                UserPageQueryInfoDO updateUserPageQueryInfoDO = new UserPageQueryInfoDO();
                updateUserPageQueryInfoDO.setUserPageQueryInfoId(userPageQueryInfoDO.getUserPageQueryInfoId());
                updateUserPageQueryInfoDO.setQueryInfo(userPageQueryInfoDTO.getQueryInfo());
                userPageQueryInfoService.updateUserPageQueryInfoByUserPageQueryInfoId(updateUserPageQueryInfoDO);
            } else {
                UserPageQueryInfoDO insertUserPageQueryInfoDO = new UserPageQueryInfoDO();
                insertUserPageQueryInfoDO.setUserId(userPageQueryInfoDTO.getUserId());
                insertUserPageQueryInfoDO.setQueryName(userPageQueryInfoDTO.getQueryName());
                insertUserPageQueryInfoDO.setDisplayInfoId(userPageQueryInfoDTO.getDisplayInfoId());
                insertUserPageQueryInfoDO.setQueryInfo(userPageQueryInfoDTO.getQueryInfo());
                userPageQueryInfoService.insertUserPageQueryInfo(insertUserPageQueryInfoDO);
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            return ResultHelper.buildResultDOByException(e);
        }
        return resultDO;
    }

    @Override
    public ResultDO<Integer> batchDeleteUserPageQueryInfoList(List<Long> userPageQueryInfoIdList) {
        ResultDO<Integer> resultDO = new ResultDO<>();
//        List<UserPageQueryInfoDO> list = new ArrayList<>();
//        for (Long primaryKey : userPageQueryInfoIdList) {
//            UserPageQueryInfoDO userPageQueryInfoDO = new UserPageQueryInfoDO();
//            userPageQueryInfoDO.setUserPageQueryInfoId(primaryKey);
//            userPageQueryInfoDO.setIsDelete("Y");
//            list.add(userPageQueryInfoDO);
//        }
//        int number = userPageQueryInfoService.batchUpdateUserPageQueryInfoList(list).size();
//        resultDO.setDomain(number);
        return resultDO;
    }

}
